package com.android.common.mvp.view.webview;

import android.app.Activity;
import android.app.DownloadManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import android.webkit.JavascriptInterface;
import com.android.common.base.BaseEvent;
import com.android.common.constant.Constant;
import com.android.common.mvp.model.H5MediaSelectorBean;
import com.android.common.mvp.model.JsParameterCommonBean;
import com.android.common.utils.GsonUtil;
import com.android.common.utils.SpUtils;
import com.android.common.utils.media.MediaSelectorUtils;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnResultCallbackListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.greenrobot.eventbus.EventBus;

/**
 * h5-原生互调模版
 */
public abstract class CommonWebViewJsBridge implements BaseWebViewJsBridge{

    protected Activity mContext;

    protected BaseWebView mWebView;
    /**
     *
     * @param methodName 操作名称
     * @param parameter  操作参数json格式
     * @param callBackMethodName   回调h5
     */
    @JavascriptInterface
    public void jsCall(final String methodName,final String parameter,final String callBackMethodName){
        if (Constant.GO_BACK.equals(methodName)) {
            EventBus.getDefault().post(new BaseEvent(BaseEvent.WEB_VIEW_GO_BACK));
        } else if (Constant.CLOSE_PAGE.equals(methodName)) {
            EventBus.getDefault().post(new BaseEvent(BaseEvent.WEB_VIEW_CLOSE_PAGE));
        } else if (Constant.LOGIN.equals(methodName)) {
            EventBus.getDefault().post(new BaseEvent(BaseEvent.LOGIN));
        } else if (Constant.SELECT_MEDIA.equals(methodName)){
            h5SelectMedias(parameter,callBackMethodName);
        }else if (Constant.DOWN_LOAD.equals(methodName)) {
            JsParameterCommonBean bean = GsonUtil.StringToObject(parameter, JsParameterCommonBean.class);
            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(bean.getUrl()));
            request.addRequestHeader(Constant.TOKEN_KEY, SpUtils.getString(Constant.TOKEN_KEY));
            request.allowScanningByMediaScanner();
            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, bean.getFileName());
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            DownloadManager dm = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
            dm.enqueue(request);
            Intent pageView = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
            pageView.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mContext.startActivity(pageView);
        }
    }

    private void h5SelectMedias(final String parameter,final String callBackMethodName){
        H5MediaSelectorBean h5MediaSelectorParameter = GsonUtil.StringToObject(parameter, H5MediaSelectorBean.class);
        int mediaType = h5MediaSelectorParameter.getMediaType();
        int maxSelectPicNum = h5MediaSelectorParameter.getMaxSelectPicNum();
        int maxSelectVideoNum = h5MediaSelectorParameter.getMaxSelectVideoNum();
        int maxFileSize = h5MediaSelectorParameter.getMaxFileSize();
        List<LocalMedia> selectedMedias = h5MediaSelectorParameter.getSelectedList();
        MediaSelectorUtils.selectMedia(mContext, mediaType, maxSelectPicNum + maxSelectVideoNum, maxSelectVideoNum, maxFileSize, selectedMedias, new OnResultCallbackListener<LocalMedia>() {
            @Override
            public void onResult(ArrayList<LocalMedia> result) {
                Log.d("wb005",GsonUtil.ObjectToString(result));
                Uri uri = Uri.fromFile(new File(result.get(0).getRealPath()));
                mWebView.loadUrl("javascript:+"+ callBackMethodName+ "(" + "'" + uri+ "'" + ")");
            }

            @Override
            public void onCancel() {

            }
        });
    }

    public Activity getContext() {
        return mContext;
    }

    public void setContext(Activity context) {
        mContext = context;
    }

    public BaseWebView getWebView() {
        return mWebView;
    }

    public void setWebView(BaseWebView webView) {
        mWebView = webView;
    }

}
